package mapkit.layers
{
	import flash.geom.Point;

	import mx.core.IFactory;
	import mx.core.UIComponent;
	import mx.graphics.RectangularDropShadow;

	public class MapTip extends MapGraphic
	{
		private var _rendererChanged:Boolean=false;
		private var _renderer:IFactory=null;

		public function get contentRenderer():IFactory
		{
			return _renderer;
		}

		public function set contentRenderer(value:IFactory):void
		{
			_renderer=value;
			_rendererChanged=true;
			invalidateProperties();
		}

		private var _footSize:Number=10;

		public function get footSize():Number
		{
			return _footSize;
		}

		public function set footSize(value:Number):void
		{
			_footSize=value;
		}

		override public function get anchor():Point
		{
			return new Point(x + width / 2, y + height);
		}

		override public function set anchor(value:Point):void
		{
			x=value.x - width / 2;
			y=value.y - height;
		}

		public function MapTip()
		{
			super();
			width=100;
			height=40;
		}

		private var _content:UIComponent=null;

		override protected function commitProperties():void
		{
			super.commitProperties();
			var rawContent:Object=null;
			if (_rendererChanged)
			{
				if (!_content && _renderer)
				{
					rawContent=_renderer.newInstance();
					rawContent.data=this;
					if (rawContent is UIComponent)
					{
						_content=rawContent as UIComponent;
					}
				}
				if (_content)
				{
					if (contains(_content))
					{
						removeChild(_content);
					}
					_content.percentWidth=100;
					_content.percentHeight=100;
					addChild(_content);
				}
				_rendererChanged=false;
			}
		}

		private var dropShadow:RectangularDropShadow=new RectangularDropShadow();

		protected function updateBackground():void
		{
			var bothick:int=getStyle("borderThickness");
			var bocolor:int=getStyle("borderColor");
			var boalpha:Number=getStyle("borderAlpha");
			var bacolor:int=getStyle("backgroundColor");
			var baalpha:Number=getStyle("backgroundAlpha");

			graphics.clear();
			dropShadow.drawShadow(graphics, 0, 0, width - dropShadow.distance, height -
				_footSize - dropShadow.distance);
			graphics.lineStyle(bothick, bocolor, boalpha);
			graphics.beginFill(bacolor, baalpha);
			graphics.moveTo(0, 0);
			graphics.lineTo(width - dropShadow.distance, 0);
			graphics.lineTo(width - dropShadow.distance, height - _footSize - dropShadow.distance);
			graphics.lineTo(width / 2 + _footSize / 2 + 0.5 * dropShadow.distance,
				height - _footSize - dropShadow.distance);
			graphics.lineTo(width / 2, height);
			graphics.lineTo(width / 2 - _footSize / 2 - 0.5 * dropShadow.distance,
				height - _footSize - dropShadow.distance);
			graphics.lineTo(0, height - _footSize - dropShadow.distance);
			graphics.lineTo(0, 0);
			graphics.endFill();
		}

		protected function get contentX():Number
		{
			return 0;
		}

		protected function get contentY():Number
		{
			return 0;
		}

		protected function get contentWidth():Number
		{
			return width - dropShadow.distance;
		}

		protected function get contentHeight():Number
		{
			return height - _footSize - dropShadow.distance;
		}

		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
		{
			super.updateDisplayList(unscaledWidth, unscaledHeight);

			updateBackground();

			if (_content)
			{
				_content.x=contentX;
				_content.y=contentY;
				_content.width=contentWidth;
				_content.height=contentHeight;
			}
		}
	}
}